import { ref, computed, reactive, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { emitter } from "@/utils/mitt";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import {
  SecuritiesMyStrategySendStrategy,
  SecuritiesMyStrategyGetMyAllStrategyData
} from "@/api/stu/Cecurity";
import qs from "qs";
export function useMyStrategy() {
  const route = useRoute()
  const loading = ref<boolean>(false);
  const btnloading = ref<boolean>(false);
  const DialogVisible = ref<boolean>(false);
  const dataList = ref();
  const chosedom = ref(false);
  const strategy = ref();
  const SetStrayegy = ref();
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "策略类型",
      prop: "strategyType"
    }, {
      label: "策略名称",
      prop: "strategyName",
    }, {
      label: "交易所",
      prop: "bourse"
    }, {
      label: "选股指标",
      prop: "stockIndexName"
    }, {
      label: "回测开始时间",
      prop: "beginTime"
    }, {
      label: "回测结束时间",
      prop: "endTime"
    }, {
      label: "初始化金额",
      prop: "beginAmount"
    }, {
      label: "回测频率",
      prop: "funcRate"
    }, {
      label: "状态",
      prop: "states"
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const Tactics = [
    { label: "股票策略", value: 0 },
    { label: "期货策略", value: 1 }
  ]
  const NewTactics = ref<any>()
  // 新建策略
  async function newchose() {
    if (NewTactics.value === 0 || NewTactics.value === 1) {
      DialogVisible.value = false;
      chosedom.value = true;
    } else {
      ElMessage.warning("请选择策略类型")
      return
    }
  }

  // 关闭选择策略窗口
  function CloseVisible() {
    NewTactics.value = "";
    DialogVisible.value = false;
  }

  // 关闭策略
  emitter.on("BackStrategy", () => {
    chosedom.value = false;
    NewTactics.value = "";
  })

  async function TransferTip(row, nums) {
    // 1:查看 2:提交
    if (nums) {
      SetStrayegy.value.onDetails(row)
      chosedom.value = true
    } else {
      const data = qs.stringify({
        strategyId: row.strategyId,
        "planId": route.query.planId,
        "taskId": route.query.taskId
      })
      btnloading.value = true;
      const res = await SecuritiesMyStrategySendStrategy(data);
      if (res.success) {
        ElMessage.success(res.msg)
        btnloading.value = false;
        GetDataList()
      } else {
        ElMessage.error(res.msg);
        setTimeout(() => { btnloading.value = false; }, 1500)
      }

    }
  }
  emitter.on("GetDataList", () => { GetDataList() })
  async function GetDataList() {
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
    };
    const res = await SecuritiesMyStrategyGetMyAllStrategyData(data);
    if (res.success) {
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  function handleSizeChange() {
    GetDataList()
  }
  function handleCurrentChange() {
    GetDataList()
  }

  onMounted(() => {
    GetDataList()
  })



  return {
    loading,
    dataList,
    strategy,
    columns,
    chosedom,
    pagination,
    Tactics,
    btnloading,
    NewTactics,
    SetStrayegy,
    DialogVisible,
    TransferTip,
    handleSizeChange,
    handleCurrentChange,
    CloseVisible,
    newchose
  }
}
